//
//  GLMainViewController.swift
//  GLWeiBo
//
//  Created by mac on 16/5/9.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class GLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        setupTabBar()
        
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupComposeButton()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @objc private func clickComposeButton() {
        print("执行了")
    }
    private lazy var composeButton:UIButton = UIButton(
        title: nil,
        titleColor: UIColor.blackColor(),
        fontSize: 14,
        imageName: "tabbar_compose_icon_add",
        backgroundImageName: "tabbar_compose_button")

}
extension GLMainViewController {
    private func setupTabBar() {
        
        tabBar.shadowImage=UIImage.GL_singleImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage=UIImage(named: "tabbar_background")
        
        tabBar.addSubview(composeButton)
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
    }
    private func setupComposeButton() {
        tabBar.bringSubviewToFront(composeButton)
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
    }

}
//MARK: - 添加子控制器
extension GLMainViewController {
    //添加所有控制器
   private func addChildViewControllers() {
    addChildViewController(GLHomeViewController(), title: "首页", imageName: "tabbar_home")
    addChildViewController(GLMessageViewController(), title: "消息", imageName:
        "tabbar_message_center")
    addChildViewController(UIViewController())
    addChildViewController(GLDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
    addChildViewController(GLProfileViewController(), title: "我", imageName: "tabbar_profile")    }
    //添加控制器
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .Normal)
        let nav = UINavigationController(rootViewController: vc)
        vc.tabBarItem.selectedImage=UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置图片渲染颜色
        addChildViewController(nav)
    }
}
